package org.locker.service.impl;

import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.locker.domain.WechatMpUser;
import org.locker.domain.bo.WechatMpUserBo;
import org.locker.domain.vo.WechatMpUserVo;
import org.locker.mapper.WechatMpUserMapper;
import org.locker.service.IWechatMpUserService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 系统参数Service业务层处理
 *
 * @author winnie
 * @date 2024-05-21
 */
@RequiredArgsConstructor
@Service
public class WechatMpUserServiceImpl  extends ServiceImpl<WechatMpUserMapper, WechatMpUser>  implements IWechatMpUserService {

    private final WechatMpUserMapper baseMapper;

    /**
     * 查询系统参数
     */
    @Override
    public WechatMpUserVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询系统参数列表
     */
    @Override
    public TableDataInfo<WechatMpUserVo> queryPageList(WechatMpUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WechatMpUser> lqw = buildQueryWrapper(bo);
        Page<WechatMpUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询系统参数列表
     */
    @Override
    public List<WechatMpUserVo> queryList(WechatMpUserBo bo) {
        LambdaQueryWrapper<WechatMpUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WechatMpUser> buildQueryWrapper(WechatMpUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WechatMpUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAppId()), WechatMpUser::getAppId, bo.getAppId());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), WechatMpUser::getOpenId, bo.getOpenId());
        lqw.eq(StringUtils.isNotBlank(bo.getUnionId()), WechatMpUser::getUnionId, bo.getUnionId());
        lqw.eq(bo.getIsSubscribe() != null, WechatMpUser::getIsSubscribe, bo.getIsSubscribe());
        return lqw;
    }

    /**
     * 新增系统参数
     */
    @Override
    public Boolean insertByBo(WechatMpUserBo bo) {
        WechatMpUser add = MapstructUtils.convert(bo, WechatMpUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改系统参数
     */
    @Override
    public Boolean updateByBo(WechatMpUserBo bo) {
        WechatMpUser update = MapstructUtils.convert(bo, WechatMpUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WechatMpUser entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除系统参数
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
